package demo5;

import javax.naming.Name;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-16
 * Time: 21:00
 */
//Comparator比较器的掌握
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

//比较器
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class ScoreComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score - o2.score);
    }
}
class NameComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("lyh",18,99.99);
        students[1] = new Student("abc",10,79.99);
        students[2] = new Student("gb",6,59.99);
        NameComparator nameComparator = new NameComparator();
        System.out.println("排序前"+Arrays.toString(students));
        Arrays.sort(students,nameComparator);
        System.out.println("排序后"+Arrays.toString(students));
    }
    public static void main5(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("lyh",18,99.99);
        students[1] = new Student("abc",10,79.99);
        students[2] = new Student("gb",6,59.99);
        ScoreComparator scoreComparator = new ScoreComparator();
        System.out.println("排序前"+Arrays.toString(students));
        Arrays.sort(students,scoreComparator);
        System.out.println("排序后"+Arrays.toString(students));
    }
    public static void main4(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("lyh",18,99.99);
        students[1] = new Student("abc",10,79.99);
        students[2] = new Student("gb",6,59.99);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println("排序前"+Arrays.toString(students));
        Arrays.sort(students,ageComparator);
        System.out.println("排序后"+Arrays.toString(students));
    }

    public static void main3(String[] args) {
        Student student1 = new Student("lyh",18,99.99);
        Student student2 = new Student("abc",10,79.99);

        ScoreComparator scoreComparator = new ScoreComparator();
        int ret = scoreComparator.compare(student1,student2);
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        Student student1 = new Student("lyh",18,99.99);
        Student student2 = new Student("abc",10,79.99);

        AgeComparator ageComparator = new AgeComparator();
        int ret = ageComparator.compare(student1,student2);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        Student student1 = new Student("lyh",18,99.99);
        Student student2 = new Student("abc",10,79.99);
        int ret = student1.compareTo(student2);
        System.out.println(ret);
    }
}
